package com.qf.xiaomi.web.servlet;

import com.qf.xiaomi.pojo.Cart;
import com.qf.xiaomi.pojo.Goods;
import com.qf.xiaomi.pojo.User;
import com.qf.xiaomi.service.CartService;
import com.qf.xiaomi.service.GoodsService;
import com.qf.xiaomi.service.impl.CartServiceImpl;
import com.qf.xiaomi.service.impl.GoodsServiceImpl;
import com.qf.xiaomi.web.base.BaseServlet;

import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.List;

/**
 * @author 千锋教育
 * @Company http://www.mobiletrain.org/
 * @Version 1.0
 */
@WebServlet("/cartservlet")
public class CartServlet extends BaseServlet {

    CartService service = new CartServiceImpl();

    /**
     * 清空购物车
     * @param req
     * @param resp
     * @throws Exception
     */
    public void clearCartAjax(HttpServletRequest req, HttpServletResponse resp ) throws  Exception {
        //获取当前登录的用户
        User user = (User) req.getSession().getAttribute("user");
        service.clearCart(user.getId());

    }

    public void addCartAjax(HttpServletRequest req, HttpServletResponse resp ) throws  Exception {
        //获取当前登录的用户
        User user = (User) req.getSession().getAttribute("user");
        //获取 前端参数
        int goodsId = Integer.parseInt(req.getParameter("goodsId"));
        int number = Integer.parseInt(req.getParameter("number"));
        //如果number = 0，删除 该商品对应的购物车
        if(number == 0) {
            service.deleteCart(goodsId, user.getId());
        }else {
            //查询商品对象
            GoodsService goodsService = new GoodsServiceImpl();
            Goods goods = goodsService.findById(goodsId);

            //根据用户id和商品id查询购物车对象
            Cart cart = service.findCartByUIdAndGoodsId(user.getId(), goodsId);
            //如果cart = null， 说明该用户没有添加此商品到购物车，则新增到购物车
            if(cart == null) {
                cart = new Cart();
                cart.setId(user.getId());
                cart.setPid(goodsId);
                cart.setNum(1);
                cart.setMoney(goods.getPrice());

                service.insert(cart);
            }
            //如果cart ！= null， 说明该用户已经添加此商品到购物车，则更新购物车
            else {
                cart.setNum(cart.getNum() + number);
                cart.setMoney(cart.getNum() * goods.getPrice());

                service.update(cart);
            }
        }



    }


    /**
     * 获取购物车
     *
     *
     *  key: carts
     * @param req
     * @param resp
     * @return
     * @throws Exception
     */
    public String getCart(HttpServletRequest req, HttpServletResponse resp ) throws  Exception {
        //获取当前登录的用户
        User user = (User) req.getSession().getAttribute("user");
        //调用service，查询购物车

        List<Cart> carts = service.getCarts(user.getId());
        //存储到 request域
        req.setAttribute("carts", carts);

        return "forward:/cart.jsp";
    }
    /**
     * 添加到 购物车
     *
     *  先判断该用户是否在购物车中 添加了某商品
     *      如果添加了， 则修改
     *      如果没有添加，则新增
     * @param req
     * @param resp
     * @return
     * @throws Exception
     */
    public String addCart(HttpServletRequest req, HttpServletResponse resp ) throws  Exception {
        //获取请求参数
        int goodsId = Integer.parseInt(req.getParameter("goodsId"));
        int number = Integer.parseInt(req.getParameter("number"));
        //从session中获取 用户信息
        User user = (User) req.getSession().getAttribute("user");

        //查询某用户在购物车是否存在某商品
        //根据用户id和商品id查询 购物车对象
        Cart cart = service.findCartByUIdAndGoodsId(user.getId(), goodsId);
        //根据商品id，查询商品信息
        GoodsService goodsService = new GoodsServiceImpl();
        Goods goods = goodsService.findById(goodsId);
        //获取商品的架构
        double price = goods.getPrice();

        //如果cart = null， 则说明该用户的购物车不存在此商品， 添加到购物车
        if(cart == null){
            //创建购物车对象
            cart = new Cart();
            //用户id
            cart.setId(user.getId());
            cart.setPid(goodsId);
            cart.setNum(number);
            cart.setMoney(number * price);

            service.insert(cart);
        }
        //否则说明该用户的购物车存在此商品，修改购物车的数量和总价
        else {
            cart.setNum(cart.getNum() + number);
            cart.setMoney(cart.getNum() * price);

            service.update(cart);
        }


        return "redirect:/cartSuccess.jsp";
    }
}
